﻿// GoToLocation.js
/// <reference assembly="System.Web.Extensions" name="MicrosoftAjax.js"/>

Type.registerNamespace('ADF.Samples');

// base object for gotolocation
ADF.Samples.goToLocationClass = function (element) {
    ADF.Samples.goToLocationClass.initializeBase(this,[element]);
    this._panel = null;
    this._displayX = null;
    this._displayY = null;
    this._panelID = null;
    this._initialPositionSet = false;
}

ADF.Samples.goToLocationClass.prototype = {
    initialize: function () {
        ADF.Samples.goToLocationClass.callBaseMethod(this, 'initialize');
        var id = this.get_id();
        this._displayX = $get(id + "_GoToLocationX");
        this._displayY = $get(id + "_GoToLocationY");
        if (this._panelID && this._panelID.length > 0)
            this._panel = $get(this._panelID);
        if (this._panel != null)
            this._panel.style.zIndex = "10000";
        else if (this._map != null)
            this._map.add_extentChanged(Function.createDelegate(this, this._updateCoords));
        this._updateCoords();
       // alert("Thong");
    },

    get_map: function () {
        /// <value name="map" type="Object">Map control associated with the GoToLocation functionality</value>
        return this._map;
    },
    set_map: function (value) {
        this._map = value;
    },

    pan: function (zoom) {
        /// <summary>
        /// Pans to location, with option to zoom in.
        /// </summary>
        /// <param name="zoom" type="Boolean">Optional. If true, function will zoom in. Defaults to false./param>
        if (zoom == null) zoom = false;
        var m = this.get_map();
        var x = this._displayX.value;
        var y = this._displayY.value;
        var maxExtent = m.get_layers().get_extent();
        if (isNaN(x) || isNaN(y)) {
            alert("Cả 2 giá trị đều là kiểu số.");
        } else if (maxExtent.get_xmin() > x || maxExtent.get_xmax() < x || maxExtent.get_ymin() > y || maxExtent.get_ymax() < y) {
            var blurb = "Tọa độ nằm ngoài phạm vi của tất cả các lớp bản đồ. Hãy nhập tọa độ trong giới hạn cho phép:\n";
            blurb += "Min X: " + maxExtent.get_xmin() + "\n";
            blurb += "Min Y: " + maxExtent.get_ymin() + "\n";
            blurb += "Max X: " + maxExtent.get_xmax() + "\n";
            blurb += "Max Y: " + maxExtent.get_ymax() + "\n";
            alert(blurb);
        } else {
            var point = new ESRI.ADF.Geometries.Point(parseFloat(this._displayX.value), parseFloat(this._displayY.value));
            if (zoom) {
                var width = m.get_extent().get_width() * 0.25;
                var height = m.get_extent().get_height() * 0.25;
                var box = new ESRI.ADF.Geometries.Envelope(point.get_x() - width, point.get_y() - height, point.get_x() + width, point.get_y() + height);
                m.zoomToBox(box);
            } else
                m.panTo(point);
//            if (this._panel != null) {
//                var gtxydialog = $find(this._panel.id);
//                gtxydialog.hide();
//            }
        }
    },

    get_panelID: function () {
        return this._panelID;
    },
    set_panelID: function (value) {
        this._panelID = value;
    },


    toggle: function () {
        /// <summary>
        /// Toggles visiblity of dialog
        /// </summary>
        this._updateCoords();
        if (this._panel != null) {
            var gtxydialog = $find(this._panel.id);
            gtxydialog.toggleVisibility();
        }
    },

    _updateCoords: function () {
        var center = this.get_map().get_center();
        // get center coords of map to put into the xy textboxes
        this._displayX.value = Math.round(center.get_x() * 1000) / 1000;
        this._displayY.value = Math.round(center.get_y() * 1000) / 1000;
    }


}

ADF.Samples.goToLocationClass.registerClass('ADF.Samples.goToLocationClass', Sys.UI.Control);

if (typeof(Sys) !== "undefined") { Sys.Application.notifyScriptLoaded(); }


